<?php
//安全卡生成类
class ECardCore 
{

    private $_uid = 0;
    //序列化之后的坐标值

    private $_code = NULL;
    //sn,唯一码
    private $_sn = NULL;
    //字母
    private $letter = 'ABCDEFGH';
    private $CI = NULL;
    private $property = array('uid','sn','code','expire','username');
    public function __construct()
    {
        /*if(is_array($code))
        {
            $this->generateCode($code['code']);
            $this->generateSN($code['sn']);
        }
        else
        {
            $this->generateCode($code);
            $this->generateSN($sn);
        }
        $this->uid=$uid;*/
        $this->CI =& get_instance();

    }

    public function __get($key)
    {
        if(in_array($key, $this->property))
            return $this->$key;
        return NULL;
    }
    public function __set($key,$value)
    {
        if(in_array($key, $this->property))
            $this->$key = $value;
    }



    
    //生成数字
    private function rand_num($num){


        $array = str_split(date('ndH'). substr(time(), -4) . substr(microtime(), 2, 3) . sprintf('%02d', rand(0, 99)),1);


        shuffle($array);
        $key   =   array_rand($array,$num);
        $str   =   "";
        for($i=0;$i <$num;$i++){
            $str .=$array[$key[$i]];
        }
        return $str;
    }
    

    public function generateCode()
    {

        if(!isset($this->code))
        {
            $arr   = array();
            for($k=0;$k<strlen($this->letter);$k++){
                for ($i = 0;$i < 10;$i++){
                    $rand = $this->rand_num(2);
                    $arr[$this->letter{$k}.$i] =  $rand;
                }
            }
            $this->code = serialize($arr);
        }
    }
    
    public function generateSN()
    {
        
        if(!isset($this->sn))
        {
            $b = date('Y').(isset($this->uid) ? $this->uid.$this->rand_num(14-strlen($this->uid)) : $this->rand_num(14));
            $this->sn = $b;
        }

        
        $this->sn =  implode(str_split($this->sn,4),'  ');
        
    }
    public function __toString()
    {
        return $this->display();
    }
    public function display(){
            $codes = unserialize($this->code);
            //图片初始值
            $height = 370;    //图片高度
            $width =   354;    //图片宽度
            $im = imagecreatetruecolor($width,$height);
            $linecolor = imagecolorallocate($im, 229,229,229);
            $fontcolor = imagecolorallocate($im, 0, 0, 0);
            $top_rectangle_color = imagecolorallocate($im,241,254,237);
            $top_letter_color = imagecolorallocate($im,54,126,76);
            $left_rectangle_color = imagecolorallocate($im,243,247,255);
            $left_num_color = imagecolorallocate($im,4,68,192);
            $logo_str_color = imagecolorallocate($im,0,0,0);
            imagefill($im,0,0,imagecolorallocate($im,255,255,255));  //图片背景色
            $font    = APPPATH.'./font/arial.ttf';  //字体
            $font_en = APPPATH.'./font/candarab.ttf'; //英文字体
            $font2   = APPPATH.'./font/MSYH.ttf'; //密保卡上方黑体

            imageline($im,10,40,$width-10,40,$linecolor);
            $ltext = "电子密保卡";
            if(!imagettftext($im,13,0,130,30,$logo_str_color,$font2,$ltext)) {
                exit('error');
            }
            $x = 20; $y = 65; //序列号位置
            imagettftext($im,10,0,$x,$y,$color,$font2,'sn:');
            imagettftext($im,11,0,$x+20,$y,$color,$font_en,$this->sn);
            if($this->expire)
            {
                imagettftext($im,10,0,$x+180,$y,$color,$font2,'有效期:');
                imagettftext($im,11,0,$x+230,$y,$color,$font_en,$this->expire);
            }
            //颜色框
            imagefilledrectangle($im,10,106,$width-10,80,$top_rectangle_color);
            imagefilledrectangle($im,10,105,65,$height-25,$left_rectangle_color);
            //写入最上排英文字母及竖线
            for($i=1;$i<=8;$i++){
                $x = $i*35+43;
                 $y = 100;
                  $float_size = 11;   //字母位置参数
                imagettftext($im,$float_size,0,$x,$y,$top_letter_color,$font_en,$this->letter{$i-1});//写入最上排英文字母
            }
            for($i=0;$i<=7;$i++){
                $linex = $i*35+65;    $liney = 105;    $liney2 = $height-25;  //竖线位置参数
                imageline($im,$linex,$liney,$linex,$liney2,$linecolor);//划入竖线
            }
            //写入竖排数字及填入矩阵数据 划横线
            for($j=0;$j<10;$j++){
                $jj=$j+1;
                $x=35;  $y=($jj*24)+100; //左排数字及横线位置参数
                imagettftext($im, $float_size, 0, $x, $y, $left_num_color, $font_en, $j);//写入左排数字
                for($i=1;$i<=10;$i++){
                    $float_size2=11;  $x = $i*35+40;  $sy=$y;  //填入矩阵数据位置参数
                    $s = $this->letter{$i-1};
                    $s .= $j;
                    imagettftext($im,$float_size2,0,$x,$sy,$fontcolor,$font_en,$codes[$s]);//写入矩阵数据
                }
            }
            for($j=0;$j<11;$j++){
                $line_x=10; $line_x2=$width-10;$y=$j*24+105; //横线位置参数 y坐标数据同上
                imageline($im,$line_x,$y,$line_x2,$y,$linecolor);//划入横线
            }
            //外框边线
            imageline($im,10,10,$width-10,10,$linecolor);
            imageline($im,10,10,10,$height-25,$linecolor);//竖线
            imageline($im,$width-10,10,$width-10,$height-25,$linecolor);
            //生成图片
            ob_clean();
            header("Content-type: image/jpeg");
            imagejpeg($im,null,100);
            imagedestroy($im);
        }

}
?>
